<template>
    <div class="edit-popup" name="myleave">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="姓名">
                    <el-input v-model="userStore.userInfo.realname" disabled />
                </el-form-item>
                <el-form-item label="单位">
                    <el-input v-model="userStore.userInfo.deptname" disabled />
                </el-form-item>
                <el-form-item label="职务">
                    <el-input v-model="userStore.userInfo.PositionName" disabled />
                </el-form-item>
                <el-form-item label="工作时间">
                    <el-input v-model="userStore.userInfo.JoinWorkDateTime" disabled />
                </el-form-item>
                <el-form-item label="工作年限">
                    <el-input v-model="userStore.userInfo.WorkYears" disabled />
                </el-form-item>
                <el-form-item label="职务类别">
                    <el-input v-model="userStore.userInfo.PositionTypeName" disabled />
                </el-form-item>
                <el-form-item label="请假类别" prop="holiday">
                    <el-select class="w-[280px]" v-model="formData.holiday">
                        <el-option v-for="(item, key) in optionData.holiday" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="请假事由" prop="memo">
                    <el-input v-model="formData.memo" placeholder="请输入请假事由" clearable />
                </el-form-item>
                <el-form-item label="请假时间" prop="leaveTime">
                    <el-date-picker v-model="formData.leaveTime" type="daterange" range-separator="至"
                        start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD" />

                </el-form-item>
                <el-form-item label="是否离连">
                    <el-switch v-model="formData.isout" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="上传附件" prop="filelist">
                    <div>
                        <el-text v-for="(itemlink, linkkey) in formData.filelist" :key="linkkey">
                            <el-link :href="itemlink.uri" target="_blank">{{ itemlink.name }}</el-link>
                            <el-button @click="onDelFile(linkkey)">删除</el-button>
                        </el-text>
                        <upload type="file" @success="onUploadSuccess" :show-progress="true">
                            <el-button type="primary">上传附件</el-button>
                        </upload>
                        <div class="form-tips">支持文件格式:图片.jpg .png 文档：word ppt excel pdf</div>
                    </div>
                </el-form-item>

            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="myleave">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import useUserStore from '@/stores/modules/user'
import { useDictData } from '@/hooks/useDictOptions'
import Upload from '@/components/upload/index.vue'
import { leaveAdd, leaveEdit, leaveDetail } from '@/api/leave'
const userStore = useUserStore()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const { dictData: optionData } = useDictData('Position,PositionType,holiday')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑请假' : '新增请假'
})
/**从文件列表中删除文件 */
const onDelFile = (index: any) => {
    formData.filelist.splice(index, 1)
}
//const filelist = ref<any[]>([])
const onUploadSuccess = (file: any) => {
    formData.filelist.push(file.data);
    console.log('文件上传成功', file.data)
    // console.log('用户列表数据', pager.lists)
    // Object.keys(pager.lists).forEach((key) => {
    //     if (pager.lists[key]['id'] == file.data['mid']) {
    //         pager.lists[key]['avatar'] = file.data['uri']
    //     }
    // })
    //getLists()
}
const formData = reactive({
    id: '',
    mid: userStore.userInfo.id,
    holiday: '',
    memo: '',
    leaveTime: '',
    isout: 0,
    filelist: [] as any
})

const formRules = {
    holiday: [
        {
            required: true,
            message: '请假类别必须选择',
            trigger: ['blur']
        },

    ],
    memo: [
        {
            required: true,
            message: '请假原因必须填写',
            trigger: ['blur']
        },

    ],
    leaveTime: [
        {
            required: true,
            message: '请假时间必须填写',
            trigger: ['blur']
        },

    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await leaveEdit(formData) : await leaveAdd(formData)
    console.log('提交信息', formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await leaveDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
